package pt.iscte.pidesco.app.internal;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;

import pt.iscte.pidesco.extensibility.IscdeComponent;

/**
 * The activator class controls the plug-in life cycle
 */
public class PidescoAppActivator extends AbstractUIPlugin {

//	private static final String EXT = "ep";
	public static final String PLUGIN_ID;
	public static final String VIEW_ID;
	public static final String EXP_POINT_VIEW;
	
	public static final String EXP_POINT_TEAM = "pa.iscde.core.team";
	
	private static PidescoAppActivator plugin;
	static {
		PLUGIN_ID = FrameworkUtil.getBundle(PidescoAppActivator.class).getSymbolicName();
		VIEW_ID = PLUGIN_ID.concat(".view");
		EXP_POINT_VIEW = PLUGIN_ID.concat(".view");
	}
	
	public static PidescoAppActivator getInstance() {
		return plugin;
	}
	
	static class ViewComponent implements Comparable<ViewComponent>, IscdeComponent {
		final IscdeComponent component;
		final String pluginId;
		final String viewId;
		final String viewTitle;
		final Image icon;
		
		ViewComponent(IscdeComponent component, String pluginId, String viewId, String viewTitle, Image icon) {
			this.component = component;
			this.pluginId = pluginId;
			this.viewId = viewId;
			this.viewTitle = viewTitle;
			this.icon = icon;
		}
		
		static String secondaryId(String pluginId, String viewId) {
			return pluginId + "." + viewId;
		}
		
		String getSecondaryId() {
			return viewId;
		}
		
		
		@Override
		public int compareTo(ViewComponent o) {
			return viewTitle.compareTo(o.viewTitle);
		}

		@Override
		public void createContents(Composite viewArea, Map<String, Image> reg) {
			component.createContents(viewArea, reg);
		}
		
	}
	
	private Map<String, ViewComponent> components;
	
	private void loadPlugins() throws CoreException {
		components = new HashMap<String, ViewComponent>();
	
		IExtensionRegistry reg = Platform.getExtensionRegistry();	
		for(IConfigurationElement comp : reg.getConfigurationElementsFor(EXP_POINT_VIEW)) {
			IscdeComponent c = (IscdeComponent) comp.createExecutableExtension("class");
			String pluginId = comp.getContributor().getName();
			String viewId = comp.getDeclaringExtension().getUniqueIdentifier();
			String viewTitle = comp.getDeclaringExtension().getLabel();
			String iconPath = comp.getAttribute("icon");
			Image icon = null;
			if(iconPath != null && !iconPath.isEmpty()) {
				try {
					icon = imageDescriptorFromPlugin(pluginId, iconPath).createImage();
				}
				catch(RuntimeException e) {
					logPluginError(pluginId, "could not load view icon '" + iconPath + "'");
				}
			}
				
			ViewComponent vc = new ViewComponent(c, pluginId, viewId, viewTitle, icon);
			components.put(viewId, vc);
		}
	}

		
	void logPluginError(String pluginId, String message) {
		System.err.println(pluginId + ": " + message);
	}
	
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
		loadPlugins();
	}

	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}
	

	ViewComponent getComponent(String viewId) {
		return components.get(viewId);
	}
	
	

	

	/**
	 * Returns an image descriptor for the image file at the given
	 * plug-in relative path
	 *
	 * @param path the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return imageDescriptorFromPlugin(PLUGIN_ID, path);
	}

	public Collection<ViewComponent> getComponents() {
		return Collections.unmodifiableCollection(components.values());
	}

	
	
}
